<html>
<head>
<meta http-equiv="X-UA-Compatible" content="chrome=IE8">
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>开根号算法</title>
</head>
<body>
	<script type="text/javascript" src="../libs/minicomps.min.js"></script>
	<script>
		const { Toggle, HSlider,VSlider,Panel, Canvas ,Button,TextArea,TextBox,TextInput,Label,Window,LabelInput,TipsManager,RadioButton,ColorPicker,TabbarWindow,Checkbox,Knob,Stage,MoonTextArea,Dropdown,TabbarButton,ProgressBar} = mc
		const NOSPRT='不能开根号';
		const VALMAX=9999;
		const width = Stage.width;
		const height = Stage.height;
		const off=10;
		const panel = new Panel(document.body, off, off, width-off*3,height-off*3);
		let input=new LabelInput(panel,10,10,'输入值');
		let output=new LabelInput(panel,160,10,'输出值');
		new Button(panel,310,10,'开根号',()=>{
			let num=Number(input.input.text);
			let answer=mathSqrt(num);
			output.input.text=answer;
		})
		function mathSqrt(num){
			let val=Math.sqrt(num);
			if(isInteger(val)){
				return val;
			}else{
				for(let i=2;i<=9;i++){
					let v1=num/i;
					let v2=Math.sqrt(v1);
					if(isInteger(v2)){
						return v2+'√'+i;
					}
				}
			}
			return NOSPRT;
		}
		function isInteger(obj) {
			return typeof obj === 'number' && obj%1 === 0;
		}
		//-----------
		//(new ProgressBar(panel, 10, 35, 0, 0)).setSize(800,2);
		var tip='输入值：可输入任意大于0的数值，输出值：是输入值开根号的值，√ 符号表示开根号';
		tip+='\n最小值：大于1小于9999的值，最大值：不能大于9999，√ 符号表示开根号';
		tip+='\n开始计算会把最小值到最大值之间可以开根号的所有值计算出来';
		(new TextArea(panel,420,10,tip)).setSize(width-460)
		//-----------
		let valMin=new LabelInput(panel,10,40,'最小值');
		let valMax=new LabelInput(panel,160,40,'最大值');
		let textArea=new MoonTextArea(panel,10,70,400,height-110);
		var str='{';
		valMin.input.text='1';valMax.input.text='999';
		new Button(panel,310,40,'开始计算',()=>{
			textArea.text='';
			let min=Number(valMin.input.text);
			let max=Number(valMax.input.text);
			if(max>VALMAX){
				alert('最大值只到'+VALMAX)
			}
			min=min<0?0:min;max=max>VALMAX?VALMAX:max;
			for(let i=min;i<=max;i++){
				let title=i;
				let answer=mathSqrt(title);
				if(answer!=NOSPRT){
					textArea.text+=title+'='+answer+'\n';
					//console.log(title,answer);
					str+=`"${title}":"${answer}",`
				}
			}
			str=str.substr(0,str.length-1);str+='}';
			let data=JSON.parse(str);
			//console.log(str);
			console.log(data);
		})

	</script>
</body>
</html>
